/**
 * Object Script Start
 */

(function ( $ ) {
	$.fn.svCanvas = function(options) {

		var viewCanvas = $(this);
		
		var buffCanvas = document.createElement('canvas');
		
		var defaultsOpts = {
			displayTrack: false
		};
		var opts = $.extend({}, defaultsOpts, options);
		
		var isSupportCanvas = ( opts.displayTrack && !!window.HTMLCanvasElement );
		
		var geometryObjList = new Array();
		
		var buffCtx = null;
		var ctx = null;
		
		if(isSupportCanvas){
			ctx = this.get(0).getContext('2d');
		    buffCtx = buffCanvas.getContext('2d');
		}
		
		this.clear = function(){
			if(!isSupportCanvas){
				return;
			}
			$.log.debug( 'callcanvas clear' );
			ctx.clearRect(0, 0, this.get(0).width + 1, this.get(0).height);
			buffCtx.clearRect(0, 0, buffCanvas.width + 1, buffCanvas.height);
		}
		
		this.addGeoObj = function( pGeoObj ){
			if(!isSupportCanvas){
				return;
			}
			geometryObjList.push( pGeoObj );
		}
		
		this.addGeoList = function( pGeoList ){
			if(!isSupportCanvas){
				return;
			}
			geometryObjList = geometryObjList.concat( pGeoList );
		}
		
		this.drawAll = function(){
			if(!isSupportCanvas){
				return;
			}
			this.clear();
			var geoObj = null;
			//ctx.clearRect(0, 0, buffCanvas.width + 1, buffCanvas.height);
			$.log.debug( 'geometryObjList[' + geometryObjList.length + ']' );
			while( geometryObjList.length > 0 ){
				this.draw( geometryObjList.pop() );
			}
			ctx.drawImage(buffCanvas, 0, 0);
		}
		
		this.draw = function ( pGeoObj ){
			if(!isSupportCanvas){
				return;
			}
			$.log.debug( 'geoObj[' + pGeoObj + ']' );
			if( pGeoObj.geoType == 'shape' ){
				this.drawShape( pGeoObj );
			}else{
				this.drawLine( pGeoObj );
			}
		}
		
		this.drawLine = function( pLineObj ){
			if(!isSupportCanvas){
				return;
			}
		    $.log.debug( 'callcanvas drawLine' );
		    var lvPointStart = null;
		    var lvPointEnd = null;
		    var lvLineObj = null;
		    
		    pLineObj.reset();
			buffCtx.beginPath();
			pLineObj.pointList.forEach(
				function( elm, idx ){
					if( idx == 0 ){
						buffCtx.moveTo(elm.posX, elm.posY);
					}else{
						buffCtx.lineTo(elm.posX, elm.posY);
					}
				}
			);
			buffCtx.strokeStyle = pLineObj.lineColor;
			buffCtx.stroke();
		}
	
	
		this.drawShape = function( pShapeObj ){
			if(!isSupportCanvas){
				return;
			}
		    $.log.debug( 'callcanvas drawShape' );
		    var lvPointStart = null;
		    var lvPointEnd = null;
		    var lvLineObj = null;
		    
		    pShapeObj.reset();
			buffCtx.beginPath();
			
			pShapeObj.lineList.forEach(
				function( elm, idx ){
					if( idx == 0 ){
						lvPointStart = elm.posStart;
						buffCtx.moveTo(lvPointStart.posX, lvPointStart.posY);
					}
					lvPointEnd = elm.posEnd;
					buffCtx.lineTo(lvPointEnd.posX, lvPointEnd.posY);
				}
			);
			/*
			buffCtx.fillStyle=pShapeObj.fillColor;
			buffCtx.fill();
			*/
			buffCtx.strokeStyle = pShapeObj.lineColor;
			buffCtx.stroke();
		}
		
		return this.each(function(){$this = $(this);});
	};
})(jQuery);
